//
//  TYAlignLabel.m
//
//  Created by 尼奥 on 2020/11/1.
//

#import "TYAlignLabel.h"


@interface TYAlignLabelMaker()

/// 存放对齐样式
@property (nonatomic, strong, readwrite) NSMutableArray *typeArray;

- (TYAlignLabelMakerTypeBlock)addAlignType;

@end


@interface TYAlignLabel ()

@property (nonatomic,strong) NSArray *typeArray;
@property (nonatomic,assign) BOOL hasTop;
@property (nonatomic,assign) BOOL hasLeft;
@property (nonatomic,assign) BOOL hasBottom;
@property (nonatomic,assign) BOOL hasRight;

@end

@implementation TYAlignLabel

- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines {
    CGRect textRect = [super textRectForBounds:bounds limitedToNumberOfLines:numberOfLines];
    if (!self.typeArray) {
        return textRect;
    }
    
    for (int i=0; i<self.typeArray.count; i++) {
        TYAlignLabelType type = [self.typeArray[i] integerValue];
        switch (type) {
            case TYAlignLabelType_top:
                self.hasTop = YES;
                textRect.origin.y = bounds.origin.y;
                break;
            case TYAlignLabelType_left:
                self.hasLeft = YES;
                textRect.origin.x = bounds.origin.x;
                break;
            case TYAlignLabelType_bottom:
                self.hasBottom = YES;
                textRect.origin.y = bounds.size.height - textRect.size.height;
                break;
            case TYAlignLabelType_right:
                self.hasRight = YES;
                textRect.origin.x = bounds.size.width - textRect.size.width;
                break;
            case TYAlignLabelType_center:
                if (self.hasTop) {
                    textRect.origin.x = (bounds.size.width - textRect.size.width) * 0.5;
                }
                else if (self.hasLeft) {
                    textRect.origin.y = (bounds.size.height - textRect.size.height) * 0.5;
                }
                else if (self.hasBottom) {
                    textRect.origin.x = (bounds.size.width - textRect.size.width) * 0.5;
                }
                else if (self.hasRight) {
                    textRect.origin.y = (bounds.size.height - textRect.size.height) * 0.5;
                }
                else{
                    textRect.origin.x = (bounds.size.width - textRect.size.width) * 0.5;
                    textRect.origin.y = (bounds.size.height - textRect.size.height) * 0.5;
                }
                break;
            default:
                break;
        }
    }
    return textRect;
}

- (void)drawTextInRect:(CGRect)requestedRect {
    CGRect actualRect = requestedRect;
    if (self.typeArray) {
      actualRect = [self textRectForBounds:requestedRect limitedToNumberOfLines:self.numberOfLines];
    }
    [super drawTextInRect:actualRect];
}

- (void)textAlign: (void(^)(TYAlignLabelMaker *make))alignType {
    TYAlignLabelMaker *maker = [[TYAlignLabelMaker alloc]init];
    alignType(maker);
    self.typeArray = maker.typeArray;
}

@end


@implementation TYAlignLabelMaker

- (instancetype)init {
    self = [super init];
    if (self) {
        _typeArray = [NSMutableArray array];
    }
    return self;
}

- (TYAlignLabelMakerTypeBlock)addAlignType {
    return ^(enum TYAlignLabelType type) {
        [self.typeArray addObject:@(type)];
        return self;
    };
}

- (TYAlignLabelMakerBlock)top {
    return ^(){
        return self.addAlignType(TYAlignLabelType_top);
    };
}

- (TYAlignLabelMakerBlock)left {
    return ^(){
        return self.addAlignType(TYAlignLabelType_left);
    };
}

- (TYAlignLabelMakerBlock)bottom {
    return ^(){
        return self.addAlignType(TYAlignLabelType_bottom);
    };
}

- (TYAlignLabelMakerBlock)right {
    return ^(){
        return self.addAlignType(TYAlignLabelType_right);
    };
}

- (TYAlignLabelMakerBlock)center {
    return ^(){
        return self.addAlignType(TYAlignLabelType_center);
    };
}

@end
